<?php
	
require_once("functions.php");

if(!file_exists(__DBFILE__)) {
	initDB();
}
$db=connectDB();
$rset=$db->query("SELECT id,name,games,avg,hcp,pins,score,score/games AS spg FROM bowlers ORDER BY spg DESC, avg DESC,name,id");
if(PEAR::isError($rset)) {
	print $rset->getMessage();
	print "<br>";
	exit();
}
if(checkLogin($_SERVER)) {
	$form="<form method=\"get\" action=\"addbowler.php\">";
	$form.="<tr><td colspan=\"2\">New Bowler</td><td colspan=\"2\"><input type=\"text\" size=\"10\" name=\"bowler\"></td><td colspan=\"3\" align=\"center\"><a href=\"backup.php\">Backup</a> | <a href=\"restore.php\">Restore</a></td></tr>";
	$form.="</form>";
} else {
	$form="<tr><td colspan=\"7\"><a href=\"login.php\">Login</a></td></tr>";
}
?>
<html>
<head>
<title>Bowl</title>
</head>
<body>
<table cellpadding="5" border="1" cellspacing="0" width="600">
<?php echo $form; ?>
<tr><td colspan="7" align="center"><a href="laneSheet.php">Printable Lane Score Sheet</a></td></tr>
<tr><td>Name</td><td align="right">Games</td><td align="right">Average</td><td align="right">Handicap</td><td align="right">Pins</td><td align="right">Score</td><td align="right">Score / Game</td></tr>
<?php
while(($row=$rset->fetchRow())==true) {
	$link="<a href=\"showBowler.php?id={$row[0]}\">{$row[1]}</a> <font size=\"-2\"><a href=\"delBowler.php?id={$row[0]}\">[x]</a>";
	array_shift($row);
	array_shift($row);
	$spg=array_pop($row);
	$row[]=sprintf("%.1f",$spg);
	print "<tr><td>{$link}</td>";
	foreach($row as $r) {
		print "<td align=\"right\">{$r}</td>";
	}
	print "</tr>\n";
}
?>
<tr><td colspan="7" align="center"><a href="laneSheet.php">Printable Lane Score Sheet</a></td></tr>
</table>
</body>
</html>
